/**
 *  dialogs.vala
 *
 *  Copyright (c) 2007 Marcelo Lira dos Santos <setanta@gmail.com>
 *                2007 INdT (Instituto Nokia de Technologia)
 *
 *  Author: Marcelo Lira dos Santos <setanta@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA
 */

using GLib;
using Gtk;

public class DialogDemo : GLib.Object
{
    Window window;
    Entry entry1;
    Entry entry2;
    int i = 1;

    private void
    message_dialog_clicked(Button button)
    {
        MessageDialog dialog = new MessageDialog(window,
                                                 DialogFlags.MODAL |
                                                 DialogFlags.DESTROY_WITH_PARENT,
                                                 MessageType.INFO,
                                                 ButtonsType.OK,
                                                 "This message box has been popped up the following\nnumber of times:");
        dialog.format_secondary_text(i.to_string());
        dialog.run();

        dialog.destroy();
        i++;
    }

    private void
    interactive_dialog_clicked (Button button)
    {
        Dialog dialog = new Dialog.with_buttons("Interactive Dialog",
                                                window,
                                                DialogFlags.MODAL |
                                                DialogFlags.DESTROY_WITH_PARENT,
                                                STOCK_OK,
                                                ResponseType.OK,
                                                "_Non-stock Button",
                                                ResponseType.CANCEL);

        HBox hbox = new HBox(false, 8);
        hbox.border_width = 8;
        ((VBox) dialog.vbox).pack_start(hbox, false, false, 0);

        Image stock = new Image.from_stock(STOCK_DIALOG_QUESTION,
                                           IconSize.DIALOG);
        hbox.pack_start(stock, false, false, 0);

        Table table = new Table(2, 2, false);
        table.set_row_spacings(4);
        table.set_col_spacings(4);
        hbox.pack_start(table, true, true, 0);
        Label label = new Label.with_mnemonic("_Entry 1");
        table.attach_defaults(label, 0, 1, 0, 1);
        Entry local_entry1 = new Entry();
        local_entry1.set_text(entry1.get_text());
        table.attach_defaults(local_entry1, 1, 2, 0, 1);

        label = new Label.with_mnemonic("E_ntry 2");
        table.attach_defaults(label, 0, 1, 1, 2);

        Entry local_entry2 = new Entry();
        local_entry2.set_text(entry2.get_text());
        table.attach_defaults(local_entry2, 1, 2, 1, 2);
        label.set_mnemonic_widget(local_entry2);

        hbox.show_all();
        int response = dialog.run();

        if (response == ResponseType.OK) {
            entry1.set_text(local_entry1.get_text());
            entry2.set_text(local_entry2.get_text());
        }

        //dialog.destroy();
    }

    public Window do_dialog ()
    {
        window = new Window(WindowType.TOPLEVEL);
        window.title = "Dialogs";
        window.border_width = 8;
        window.destroy += Gtk.main_quit;

        Frame frame = new Frame("Dialogs");
        window.add(frame);

        VBox vbox = new VBox(false, 8);
        vbox.border_width = 8;
        frame.add(vbox);

        /* Standard message dialog */
        HBox hbox = new HBox(false, 8);
        vbox.pack_start(hbox, false, false, 0);
        Button button = new Button.with_mnemonic("_Message Dialog");
        button.clicked += message_dialog_clicked;
        hbox.pack_start(button, false, false, 0);
        vbox.pack_start(new HSeparator(), false, false, 0);

        /* Interactive dialog*/
        hbox = new HBox(false, 8);
        vbox.pack_start(hbox, false, false, 0);
        VBox vbox2 = new VBox(false, 0);

        button = new Button.with_mnemonic("_Interactive Dialog");
        button.clicked += interactive_dialog_clicked;
        hbox.pack_start(vbox2, false, false, 0);
        vbox2.pack_start(button, false, false, 0);

        Table table = new Table(2, 2, false);
        table.set_row_spacings(4);
        table.set_col_spacings(4);
        hbox.pack_start(table, false, false, 0);

        Label label = new Label.with_mnemonic("_Entry 1");
        table.attach_defaults(label, 0, 1, 0, 1);

        entry1 = new Entry();
        table.attach_defaults(entry1, 1, 2, 0, 1);
        label.set_mnemonic_widget(entry1);

        label = new Label.with_mnemonic("E_ntry 2");

        table.attach_defaults(label, 0, 1, 1, 2);

        entry2 = new Entry();
        table.attach_defaults(entry2, 1, 2, 1, 2);
        label.set_mnemonic_widget(entry2);

        return window;
    }

    static int main (string[] args) {
        Gtk.init(out args);

        DialogDemo pd = new DialogDemo();

        var win = pd.do_dialog();
        try {
            win.set_default_icon_from_file("v-icon.png");
        } catch (GLib.Error error) {
            critical(error.message);
        }
        win.show_all();

        Gtk.main();

        return 0;
    }
}
